package com.dsbj.mp.icardservice.service.physicalcondition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.*;
import com.dsbj.mp.icardservice.mapper.EmployeeInoutRecordMapper;
import com.dsbj.mp.icardservice.mapper.EmployeeMapper;
import com.dsbj.mp.icardservice.mapper.FctoryContrastMapper;
import com.dsbj.mp.icardservice.mapper.PersontMapper;
import com.dsbj.mp.icardservice.service.EmployeeWorkStatusService;
import com.dsbj.mp.icardservice.service.FctoryContrastService;
import com.dsbj.mp.icardservice.service.PersontService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出入园区体温测量
 * </p>
 *
 */
@Service
@Transactional
public class EmployeeInoutRecordService extends ServiceImpl<EmployeeInoutRecordMapper, EmployeeInoutRecord> {

	@Resource
	private EmployeeInoutRecordMapper employeeInoutRecordMapper;

	@Resource
	private FctoryContrastMapper fctoryContrastMapper;

	@Resource
	private EmployeeMapper employeeMapper;

	@Resource
	private PersontMapper persontMapper;

	@Resource
	private EmployeeWorkStatusService employeeWorkStatusService;

	public List<FctoryContrast> getFctoryContrasts(){
		return fctoryContrastMapper.findAll();
	}

	/***
	 * 进出记录新增
	 * @param idcardno
	 * @param inoutFlag
	 * @param temperature
	 * @param createUser
	 */
	public ResponseMessage insert(String idcardno,String inoutFlag,String temperature,String createUser,String factoryContrastId,String remark){
		Map<String, Object> map=new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inoutFlag)){
			//判断员工是否可以复工
			QueryWrapper<EmployeeWorkStatus> queryWrapper = new QueryWrapper<EmployeeWorkStatus>()
					.eq("IDCARDNO",idcardno)
					.eq("STATUS",1);
			EmployeeWorkStatus employeeWorkStatus = employeeWorkStatusService.getOne(queryWrapper);
			if(null == employeeWorkStatus){
				return ResponseMessage.error("该员工未允许复工，不允通过");
			}else{
				//是否可以复工(1：是，0：否)
				if(employeeWorkStatus.getReturnToWork()==1){
					//进出标识(1：进;0：出
					int temp = Integer.parseInt(inoutFlag);
					map.put("idcardno", idcardno);
					map.put("inoutFlag", temp);
					int count = employeeInoutRecordMapper.findTenMinutData(map);
					if(count > 0 && temp == 1){
						return ResponseMessage.error("该员工十分钟内已存在进入园区的体温数据");
					}else if(count > 0 && temp == 0){
						return ResponseMessage.error("该员工十分钟内已存在出园区的体温数据");
					}else{
						//查询员工是否是第一次进
						List<EmployeeInoutRecord> employeeInoutRecords = employeeInoutRecordMapper.findByIdcardNo(idcardno);
						if( employeeInoutRecords != null && !employeeInoutRecords.isEmpty() && employeeInoutRecords.size() > 0){
							//不是第一次   进出次数加1
							EmployeeInoutRecord employeeInoutRecord = new EmployeeInoutRecord();
							employeeInoutRecord.setIdcardno(idcardno);
							if(StringUtils.isNotBlank(inoutFlag)){
								employeeInoutRecord.setInoutFlag(Integer.parseInt(inoutFlag));
							}
							if(StringUtils.isNotBlank(temperature)){
								employeeInoutRecord.setTemperature(new BigDecimal(temperature));
							}
							employeeInoutRecord.setInoutCount(employeeInoutRecords.size()+1);
							employeeInoutRecord.setCreateUser(createUser);//当前操作保安
							employeeInoutRecord.setStatus(1);
							employeeInoutRecord.setFirstInFlag(0);
							employeeInoutRecord.setRemark(remark);
							employeeInoutRecord.setFactoryContrastId(factoryContrastId);
							employeeInoutRecordMapper.insertEmployeeInoutRecord(employeeInoutRecord);
						}else{
							EmployeeInoutRecord employeeInoutRecord = new EmployeeInoutRecord();
							employeeInoutRecord.setIdcardno(idcardno);
							if(StringUtils.isNotBlank(inoutFlag)){
								employeeInoutRecord.setInoutFlag(Integer.parseInt(inoutFlag));
							}
							if(StringUtils.isNotBlank(temperature)){
								employeeInoutRecord.setTemperature(new BigDecimal(temperature));
							}
							employeeInoutRecord.setInoutCount(1);
							employeeInoutRecord.setCreateUser(createUser);
							employeeInoutRecord.setStatus(1);
							employeeInoutRecord.setFirstInFlag(1);
							employeeInoutRecord.setRemark(remark);
							employeeInoutRecord.setFactoryContrastId(factoryContrastId);
							employeeInoutRecordMapper.insertEmployeeInoutRecord(employeeInoutRecord);
						}
					}
				}else{
					return ResponseMessage.error("该员工未允许复工，不允通过");
				}
			}
		}
		return ResponseMessage.ok();
	}

	/********
	 * 二维码信息  带出附加信息
	 */
	public ResponseMessage getEmployeeInfo(String idcardno){
		EmployeeTemp employeeTemp = new EmployeeTemp();
		Employee employee = employeeMapper.findByIdcardNo(idcardno);
		if(null != employee){
			employeeTemp.setCompanyname(employee.getCompanyName());
			employeeTemp.setFirstdeptname(employee.getFirstdeptname());
			employeeTemp.setDeptname(employee.getDeptName());
			employeeTemp.setEmployeename(employee.getEmployeename());
		}else{
			//人员档案没有该身份证,附加人员信息表取
			Persont persont = persontMapper.findByIdcardNo(idcardno);
			if(null != persont){
				employeeTemp.setCompanyname(persont.getCompanyname());
				employeeTemp.setFirstdeptname(persont.getFirstdeptname());
				employeeTemp.setDeptname(persont.getDeptname());
				employeeTemp.setEmployeename(persont.getEmployeename());
			}else{
				return ResponseMessage.error("未找到该身份证号对应的员工信息");
			}
		}
		//判断是否是一次进入厂区
		List<EmployeeInoutRecord> employeeInoutRecords = employeeInoutRecordMapper.findByIdcardNo(idcardno);
		if( employeeInoutRecords.size() == 0){
			employeeTemp.setIsFirst(1);
		}else{
			employeeTemp.setIsFirst(0);
		}
		return ResponseMessage.ok(employeeTemp);
	}



}
